#!/usr/bin/python
# -*- coding: utf-8 -*-

#-------------------------------------------------
#Create At: 2009-1-29 下午10:20:13 
#Create By: Fla.sam
#
#DESC: user_manage_view
#
#-------------------------------------------------

from CoreApp.models import *
from django.conf import settings
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.forms import AdminPasswordChangeForm
from django.contrib.auth.models import User, Group
from django.forms.models import form_for_fields
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, \
    HttpResponseBadRequest
from django.shortcuts import render_to_response, get_object_or_404
from django.template import Context, Template, loader, RequestContext
from django.views.generic.list_detail import object_list, object_detail
from utils.decorators import *


@groups_required_with_403( ( 'AD_ADMIN', 'AD_SMANAGER', 'AD_MANAGER' ) )
def index( request ):
    '''
    管理员user-manage index 默认页面
    @param request:
    '''
    all_users_qs = User.objects.all()
    return object_list( request, queryset = all_users_qs, template_name = 'user_manager_index.html', paginate_by = settings.PAGINATE )

@groups_required_with_403( ( 'AD_ADMIN', 'AD_SMANAGER', 'AD_MANAGER' ) )
def user_detail( request, user_id ):
    '''
    管理员查看用户详细资料
    @param request:
    @param user_id:
    '''
    
    user_obj = get_object_or_404( User, pk = user_id )
    d = { 'user':user_obj }
    return render_to_response( 'user_manager_user_detail.html', d, context_instance = RequestContext( request ) )

@groups_required_with_403( ( 'AD_ADMIN', 'AD_SMANAGER' ) )
def user_change_status( request, user_id ):
    '''
   管理员 改变用的状态是否被锁定, 锁定->不允许登陆
    @param request:
    @param user_id:
    '''
    
    user_obj = get_object_or_404( User, pk = user_id )
    if user_obj.is_active:
        user_obj.is_active = False
    else:
        user_obj.is_active = True
    user_obj.save()
    return HttpResponseRedirect( '/user-manage/detail/%s' % user_id )

@groups_required_with_403( ( 'AD_ADMIN', 'AD_SMANAGER' ) )
def user_change_password( request, user_id ):
    '''
    管理员更改用户密码
    @param request:
    @param user_id:
    '''
    user_obj = get_object_or_404( User, pk = user_id )
    if request.method == 'POST':
        form = AdminPasswordChangeForm( user_obj, request.POST )
        d = {'form':form ,
             'user_name':user_obj}
        if form.is_valid():
            form.save()
            request.user.message_set.create( message = "修改用户密码成功" )
            return render_to_response( 'user_manage_change_password.html', d, context_instance = RequestContext( request ) )
        else:
            return render_to_response( 'user_manage_change_password.html', d, context_instance = RequestContext( request ) )
    elif request.method == 'GET':
        form = AdminPasswordChangeForm( request )
        d = {'form':form ,
             'user_name':user_obj}
        return render_to_response( 'user_manage_change_password.html', d, context_instance = RequestContext( request ) )
    else:
        return HttpResponseBadRequest()

@groups_required_with_403( ( 'AD_ADMIN', 'AD_SMANAGER', 'AD_MANAGER' ) )
def user_add(request):
    pass
